package com.wson.widget.render.compass.framework;

import java.util.ArrayList;
import java.util.Collection;

import com.wson.behavior.ctrl.IBehavior;
import com.wson.behavior.element.IClickable;
import com.wson.behavior.layout.IForm;
import com.wson.behavior.layout.ITable;
import com.wson.behavior.layout.IWindow;
import com.wson.constant.compass.IAppActionConstant;
import com.wson.constant.compass.IRuntimeConstant;
import com.wson.controller.constant.IControllerConstant;
import com.wson.model.basic.IBasicView;
import com.wson.model.clouddisk.account.ICodiskAccountMeta;
import com.wson.model.clouddisk.file.ICodiskFileMeta;
import com.wson.model.clouddisk.file.ICodiskFileView;
import com.wson.model.clouddisk.file.impl.CodiskFileModel;
import com.wson.model.compass.portfolio.ICompassPortfolioView;
import com.wson.model.compass.status.ICompassStatusMeta;
import com.wson.model.compass.status.impl.CompassStatusModel;
import com.wson.model.framework.user.impl.UserModel;
import com.wson.platform.factory.impl.BeanFactory;
import com.wson.root.util.RandomUtil;
import com.wson.util.function.HttpUtil;
import com.wson.widget.render.IRenderParam;
import com.wson.widget.render.function.sterotype.ConfirmationRender;
import com.wson.widget.render.layout.sterotype.AbstractModelExtendFormRender;

public class StatusFlowRender extends AbstractModelExtendFormRender {

	public final static String PARAM_RETWEETEDSTATUSID = "RETWEETEDSTATUSID";
	public final static String PARAM_REPLYUSERID = "REPLYUSERID";
	public final static String PARAM_FLOWID = "FLOWID";
	public final static String PARAM_INBOUNDROUTEID = "INBOUNDROUTEID";
	public final static String PARAM_INBOUNDEVENTID = "INBOUNDEVENTID";
	public final static String PARAM_PARENTFLOWID = "PARENTFLOWID";
	public final static String PARAM_CSSAVAILABLE = "CSSAVAILABLE";	
	
	@IRenderParam(name=PARAM_RETWEETEDSTATUSID)
	private Long retweetedStatusID;
	
	@IRenderParam(name=PARAM_REPLYUSERID)
	private Long replyUserID;	

	@IRenderParam(name=PARAM_FLOWID)
	private Long flowID;	
	
	@IRenderParam(name=PARAM_INBOUNDROUTEID)
	private Long inboundRouteID;
	
	@IRenderParam(name=PARAM_INBOUNDEVENTID)
	private Long inboundEventID;
	
	@IRenderParam(name=PARAM_CSSAVAILABLE)
	private Boolean cssAvailable = false;
	
	
	public Long getRetweetedStatusID() {
		return retweetedStatusID;
	}

	public void setRetweetedStatusID(Long retweetedStatusID) {
		this.retweetedStatusID = retweetedStatusID;
	}

	public Long getReplyUserID() {
		return replyUserID;
	}

	public void setReplyUserID(Long replyUserID) {
		this.replyUserID = replyUserID;
	}
	
	

	public Boolean getCssAvailable() {
		return cssAvailable;
	}

	public void setCssAvailable(Boolean cssAvailable) {
		this.cssAvailable = cssAvailable;
	}

	public Long getFlowID() {
		return flowID;
	}

	public void setFlowID(Long flowID) {
		this.flowID = flowID;
	}

	public Long getInboundRouteID() {
		return inboundRouteID;
	}

	public void setInboundRouteID(Long inboundRouteID) {
		this.inboundRouteID = inboundRouteID;
	}

	public Long getInboundEventID() {
		return inboundEventID;
	}

	public void setInboundEventID(Long inboundEventID) {
		this.inboundEventID = inboundEventID;
	}

	@Override
	public String code() {
		return "STATUSFLOW";
	}

	@Override
	public IBehavior render() {
		
		//System.out.println("getCallbackFunction "+this.getCallbackFunction());
		
		CompassStatusModel status= (CompassStatusModel)this.getModel();
		this.initModel((IBasicView)status);
		status.setRetweetStatusID(this.getRetweetedStatusID());
		
		//status.setShortText(this.getMessage(IRuntimeConstant.SYNC_REPLY));
		
		status.setInboundRouteID(this.getInboundRouteID());
		status.setInboundEventID(this.getInboundEventID());
		status.setDocumentsRequired(this.getCssAvailable());
		
		if(this.getReplyUserID()!=null){
			try{
				UserModel user = new UserModel();
				this.initModel((IBasicView)user);
				user.setID(this.getReplyUserID());
				if(user.selfSelect())
					status.setTargets(user.getName());
			}
			catch(Exception ex){
				ex.printStackTrace();
			}
		}
		
		IWindow win = BeanFactory.getCurrentFactory().getBehaviorFactory().getWindow();
		win.getAppearance().setLayout(IWindow.LAYOUT_FIT);
		
		ITable table = null;
		boolean isCSSAvailable = ((ICompassPortfolioView)this.getSessionProfile().getPortfolio()).getCodiskAccountID()!=null;
		if(isCSSAvailable){
			
			String tempfolder = this.generateTempFolder();//"TMPFOLDER"+RandomUtil.generateIdentity(8);
			DocumentsRender trender = this.initRender(new DocumentsRender());
			trender.setModel(new CodiskFileModel());
			trender.setEditable(false);
			trender.setBehaviorType(ICodiskFileMeta.BEHAVIORSTRUCTURE_BLOCKTABLE);
			trender.setEmptyText(this.getMessage(IRuntimeConstant.SYNC_NOFILES));
			trender.setParentFolder(tempfolder);
			trender.setCodiskAccountID(((ICompassPortfolioView)this.getSessionProfile().getPortfolio()).getCodiskAccountID());
			trender.setModels(new ArrayList<IBasicView>());//this.generateSampleModels());
			
			table = (ITable)trender.render();
			table.getAppearance().setType(ITable.APPEARANCETYPE_BLOCKTABLE);
			
			//table.setToolbar(null);
			
			ConfirmationRender render = this.initRender(new ConfirmationRender());
			this.initRender(render);
			render.setFramed(false);
			render.setConfirmationText(this.getMessage(IRuntimeConstant.SYNC_ADDFILESANDREFRESH));
			render.setConfirmationType(ConfirmationRender.CONFIRMACTIONTYPE_RELOAD);
			
			IClickable fileaction = trender.generateLayoutPopupAction(render
					, this.getMessage(IRuntimeConstant.SYNC_ADDFILES), false);
			fileaction.getAppearance().setType(IClickable.APPEARANCETYPE_LINK);
			fileaction.getNavigation().setHref(IAppActionConstant.ACTION_INITFOLDEREXECUTION+"?"
					+ HttpUtil.generateHttpParam(ICompassStatusMeta.SYNC_METAID, ICompassStatusMeta.META_ID)
					+ HttpUtil.generateHttpParam(IControllerConstant.REQUEST_ACTION, ICompassStatusMeta.ACTIONCODE_INITFOLDER)
					+ HttpUtil.generateHttpParam(ICompassStatusMeta.SYNC_INITIALFOLDER, tempfolder)
					+ HttpUtil.generateHttpParam(ICodiskAccountMeta.SYNC_SELF+ICodiskAccountMeta.SYNC_ID, ((ICompassPortfolioView)this.getSessionProfile().getPortfolio()).getCodiskAccountID())
					);
			fileaction.getNavigation().setTargetID("_new");
			this.addAction(fileaction);
		}
		//System.out.println("fileaction "+fileaction.generateBehaviorCode());
		IForm form = (IForm) super.render();
		
		win.addPage(form);
		
		if(table!=null)
			win.addPage(table);
		
		return win;
	}
	private String initialfolder = null;
	protected String generateTempFolder(){
		if(initialfolder==null)
			initialfolder = "TMPFOLDER"+RandomUtil.generateIdentity(8);
		return initialfolder;
	}
	
	@Override
	protected String generateSaveUrl() {
		String url =  super.generateSaveUrl();
		
		url = HttpUtil.generateURL(url, ICompassStatusMeta.SYNC_INITIALFOLDER, this.generateTempFolder());
		url = HttpUtil.generateURL(url, ICompassStatusMeta.SYNC_DOCUMENTSREQUIRED,  ((ICompassPortfolioView)this.getSessionProfile().getPortfolio()).getCodiskAccountID()!=null);
		
		System.out.println("SaveUrl "+url);
		
		return url;
	}

	@Override
	protected IClickable generateSaveAction() {
		IClickable action = super.generateSaveAction();
		action.getDepict().setTitle(this.getMessage(IRuntimeConstant.SYNC_SEND));
		//System.out.println("save "+action.generateBehaviorCode());
		
		return action;
	}
	protected  Collection<IBasicView>  generateSampleModels(){
		Collection<IBasicView> models = new ArrayList<IBasicView>();

		models.add(this.generateTestFile("xlsx", "/wscompassweb/images/codisk/xlsx.gif"));
		models.add(this.generateTestFile("docx", "/wscompassweb/images/codisk/docx.gif"));
		models.add(this.generateTestFile("pdf", "/wscompassweb/images/codisk/pdf.gif"));		
		models.add(this.generateTestFile("pptx", "/wscompassweb/images/codisk/pptx.gif"));
		
		return models;
	}
	

	
	protected  ICodiskFileView generateTestFile(String text, String icon){
		CodiskFileModel file = new CodiskFileModel();
		this.initModel((ICodiskFileView)file);
		file.setShortText(text);
		file.setIconLink(icon);
		return file;
	}
}
